package com.spring.boot.core.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * GZIP工具
 */
public abstract class GZipUtils {

	private static final int BUFFER = 1024;

	// 压缩
	public static byte[] compress(byte[] data) throws Exception {
		if (data == null || data.length == 0) {
			return data;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPOutputStream gzip = new GZIPOutputStream(out);

		gzip.write(data);
		gzip.close();

		byte[] ret = out.toByteArray();

		out.flush();
		out.close();

		return ret;
	}

	// 解压缩
	public static byte[] uncompress(byte[] data) throws Exception {
		if (data == null || data.length == 0) {
			return data;
		}
		ByteArrayInputStream in = new ByteArrayInputStream(data);
		if (in == null || in.available() == 0) {
			return new byte[0];
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPInputStream gunzip = new GZIPInputStream(in);
		byte[] buffer = new byte[BUFFER];
		int n;
		while ((n = gunzip.read(buffer)) >= 0) {
			out.write(buffer, 0, n);
		}
		gunzip.close();

		byte[] ret = out.toByteArray();
		out.flush();
		out.close();
		in.close();
		return ret;
	}

	public static byte[] uncompress(InputStream in) throws Exception {
		if (in == null || in.available() == 0) {
			return new byte[0];
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();

		GZIPInputStream gunzip = new GZIPInputStream(in);
		byte[] buffer = new byte[BUFFER];
		int n;
		while ((n = gunzip.read(buffer)) >= 0) {
			out.write(buffer, 0, n);
		}
		gunzip.close();

		byte[] ret = out.toByteArray();

		out.flush();
		out.close();

		return ret;
	}

	// 压缩
	public static String compress(String data) throws Exception {
		if (data == null || data.length() == 0) {
			return data;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPOutputStream gzip = new GZIPOutputStream(out);

		gzip.write(data.getBytes("UTF-8"));
		gzip.close();

		String base64out = Base64Utils.encoder(out.toByteArray());

		out.flush();
		out.close();

		return base64out;
	}

	// 解压缩
	public static String uncompress(String data) throws Exception {
		if (data == null || data.length() == 0) {
			return data;
		}
		byte[] byteArray = Base64Utils.decoder(data);
		ByteArrayInputStream in = new ByteArrayInputStream(byteArray);
		if (in == null || in.available() == 0) {
			return data;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPInputStream gunzip = new GZIPInputStream(in);
		byte[] buffer = new byte[BUFFER];
		int n;
		while ((n = gunzip.read(buffer)) >= 0) {
			out.write(buffer, 0, n);
		}
		gunzip.close();

		String ret = out.toString("UTF-8");
		out.flush();
		out.close();
		return ret;
	}

	public static void main(String[] args) throws Exception {
		String test = "你好hashdfahdofasdofhaosdhfoasdhiofhadsofoasdj";

		System.out.println("字符串原长:" + test.length());
		System.out.println("Compress: " + compress(test.getBytes()).length);
		System.out.println("Uncompress: " + new String(uncompress(compress(test.getBytes()))));
		System.out.println("Compress: " + compress(test).length());
		System.out.println("Uncompress: " + new String(uncompress(compress(test))));
	}
}
